package com.todod.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.PublicFileMapper;
import com.todod.mapper.PublicFileShareMapper;
import com.todod.model.PublicCatalog;
import com.todod.model.PublicFile;
import com.todod.model.PublicFileShare;

/**
 * @param
 * @author dyl
 * @date 2021-03-16
 */
@Service
@Transactional
public class PublicFileShareService {
	@Autowired
	private PublicFileShareMapper publicFileShareMapper;
	@Autowired
	public PublicCatalogService publicCatalogService;
	@Autowired
	public PublicFileService publicFileService;
	@Autowired
	public PublicFileMapper publicFileMapper;

	public void saveData(Long _userId, Long fileId, List<String> userIds, String type) {
		this.deleteByFileId(fileId);
		for (int i = 0; i < userIds.size(); i++) {
			Boolean flag = publicCatalogService.createorIsUserMOU(Long.valueOf(userIds.get(i)), fileId);
			if (flag == false) {
				PublicFile publicFile = publicFileService.selectByCodeInUse(fileId);
				if (publicFile != null) {
					save(fileId, userIds.get(i), _userId);
				} else {
					List<PublicCatalog> list = publicCatalogService.getSelfAndChildren(fileId);
					getChildren(list, userIds.get(i), _userId);
				}
			} else {
				save(fileId, userIds.get(i), _userId);
			}
		}
	}

	public List<PublicFileShare> getFileAuthorityByFileIdId(Long fileId) {
		QueryWrapper<PublicFileShare> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfs_file_id", fileId);

		List<PublicFileShare> list = publicFileShareMapper.selectList(queryWrapper);
		return list;
	}

	public PublicFileShare getAuthorityByFileId(String batchCode) {
		QueryWrapper<PublicFileShare> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfs_file_id", batchCode);

		PublicFileShare publicFileShare = publicFileShareMapper.selectOne(queryWrapper);
		return publicFileShare;
	}

	public PublicFileShare getAuthorityByFileId(Long fileId, String userId) {
		QueryWrapper<PublicFileShare> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfs_file_id", fileId);
		queryWrapper.eq("gfs_user_id", userId);

		PublicFileShare publicFileShare = publicFileShareMapper.selectOne(queryWrapper);
		return publicFileShare;
	}

	/**
	 * 
	 * @param fileId
	 */
	public void deleteByFileId(Long fileId) {
		QueryWrapper<PublicFileShare> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfs_file_id", fileId);
		List<PublicFileShare> authList = publicFileShareMapper.selectList(queryWrapper);
		for (PublicFileShare fileAuthority : authList) {
			publicFileShareMapper.deleteById(fileAuthority.getId());
		}
	}

	public List<PublicFileShare> getFileAuthorityByUserId(Long userId) {
		QueryWrapper<PublicFileShare> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfs_user_id", userId);

		List<PublicFileShare> list = publicFileShareMapper.selectList(queryWrapper);
		return list;
	}

	/*
	 * 
	 * @title deleteChildren
	 * 
	 * @description 删除文件夹下所有文件夹及文件权限
	 * 
	 * @param List<PublicCatalog>
	 * 
	 * @auth wgb
	 * 
	 * @date 2021-06-29
	 * 
	 * @return
	 */
	public void getChildren(List<PublicCatalog> list, String userId, Long _userId) {
		list.forEach(x -> {
			List<PublicFile> listFile = x.getListFile();
			if (listFile != null) {
				listFile.forEach(y -> {
					save(Long.valueOf(y.getBatchCode()), userId, _userId);
				});
			}
			if (x.getChildren2() != null) {
				getChildren(x.getChildren2(), userId, _userId);
			} else {
				if (listFile == null || listFile.size() <= 0) {
					save(x.getId(), userId, _userId);
				}
			}
		});
	}

	public void save(Long fileId, String userId, Long _userId) {

		PublicFileShare publicFileShare = getAuthorityByFileId(fileId, userId);
		if (publicFileShare == null) {
			PublicFileShare fileAuthorit = new PublicFileShare();
			fileAuthorit.setId(SnowflakeIdGenerator.getId());
			fileAuthorit.setUserId(Long.valueOf(userId));
			fileAuthorit.setFileId(fileId);
			fileAuthorit.setCreatetimeTi(LocalDateTime.now());
			fileAuthorit.setCreateuserId(_userId);

			publicFileShareMapper.insert(fileAuthorit);
		}
	}

}
